package com.zhoug.zmuidemo.ui;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.zhoug.common.utils.StringUtils;
import com.zhoug.zmui.dialog.date.ZMUIDateChooser;
import com.zhoug.zmui.dialog.gender.ZMUIGenderChooser;
import com.zhoug.zmui.round.ZMUIRoundButton;
import com.zhoug.zmuidemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class ChooserActivity extends AppCompatActivity {

    private ZMUIRoundButton mBtnGender;
    private TextView mTvGender;
    private EditText mEtMaxYear;
    private EditText mEtMaxMonth;
    private EditText mEtMaxDay;
    private ZMUIRoundButton mBtnDate;
    private TextView mTvDate;
    private ZMUIRoundButton mBtnTime;
    private TextView mTvTime;
    private ZMUIRoundButton mBtnDateTime;
    private TextView mTvDateTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chooser);
        findViews();
        mBtnGender.setOnClickListener(v -> {
            chooserGender();
        });

        mBtnDate.setOnClickListener(v->chooseDate(ZMUIDateChooser.TYPE_DATE));
        mBtnTime.setOnClickListener(v->chooseDate(ZMUIDateChooser.TYPE_TIME));
        mBtnDateTime.setOnClickListener(v->chooseDate(ZMUIDateChooser.TYPE_DATE_TIME));

    }
    private int gender;

    private void findViews() {
        mBtnGender = findViewById(R.id.btn_gender);
        mTvGender = findViewById(R.id.tv_gender);
        mEtMaxYear = findViewById(R.id.et_maxYear);
        mEtMaxMonth = findViewById(R.id.et_maxMonth);
        mEtMaxDay = findViewById(R.id.et_maxDay);
        mBtnDate = findViewById(R.id.btn_date);
        mTvDate = findViewById(R.id.tv_date);
        mBtnTime = findViewById(R.id.btn_Time);
        mTvTime = findViewById(R.id.tv_time);
        mBtnDateTime = findViewById(R.id.btn_DateTime);
        mTvDateTime = findViewById(R.id.tv_DateTime);
    }

    private void chooserGender() {
        ZMUIGenderChooser genderChooser = new ZMUIGenderChooser(this);
        genderChooser.setCallback(gender -> {
            this.gender=gender;
            mTvGender.setText(gender == ZMUIGenderChooser.GENDER_MALE ? "男" : gender == ZMUIGenderChooser.GENDER_FEMALE ? "女" : "");
        });
        genderChooser.setTitle("请选择用户的性别");
        genderChooser.setGender(gender);
        genderChooser.show();

    }
    private String year;
    private String month;
    private String day;
    private String hour;
    private String minute;
    private void chooseDate(int type){
        ZMUIDateChooser dateChooser=new ZMUIDateChooser(this);
        dateChooser.setTitle("请选择日期");

        dateChooser.setType(type);
        dateChooser.setMaxDate(getText(mEtMaxYear),getText(mEtMaxMonth),getText(mEtMaxDay));
        dateChooser.setDate(year,month,day);
        dateChooser.setTime(hour,minute);
        dateChooser.setCallback((year, month, day, hour, minute) -> {
            dateChooser.dismiss();
            this.year=year;
            this.month=month;
            this.day=day;
            this.hour=hour;
            this.minute=minute;

            if(type==ZMUIDateChooser.TYPE_DATE){
                mTvDate.setText(year+"-"+month+"-"+day);
            }else  if(type==ZMUIDateChooser.TYPE_TIME){
                mTvTime.setText(hour+":"+minute);
            } if(type==ZMUIDateChooser.TYPE_DATE_TIME){
                mTvDateTime.setText(year+"-"+month+"-"+day+" "+hour+":"+minute);
            }

        });
        dateChooser.show();
    }

    private String getText(EditText editText){
        String s = editText.getText().toString();
        if(StringUtils.isEmpty(s)){
            return null;
        }else{
            return s;
        }
    }
}